home *** CD-ROM | disk | FTP | other *** search
/ DOpus Plus / DOpus Plus.iso / Tutorial / C Guide / Simple_Module2 / IPCWindow / modinit.c < prev    next >
C/C++ Source or Header  |  1998-09-01  |  7KB  |  225 lines

  1.  
  2. /********************************************************************
  3.  
  4.  modinit.c - standard initialisation for Opus 5 modules
  5.              ( ... slightly changed by me ... :-) )
  6.  
  7.  Typically it is not required for you to change here anything,
  8.  only if you use a different compiler...
  9.  In this case you may refer also to CLib37.lha (AmiNet).
  10.  
  11.  If your module does require the datatypes library or
  12.  rexxsyslib.library, just define DATATYPES or AREXX in the compiler
  13.  options of the smakefile.
  14.  
  15.  Function: It does open/close the libraries you may need (add yours,
  16.  if they are not here) and make them available for the whole module.
  17.  
  18.  ********************************************************************/
  19.  
  20. #ifndef _DOPUS_MODULE_DEF
  21. #define _DOPUS_MODULE_DEF
  22. #include <dopus/modules.h>
  23. #endif
  24.  
  25. #ifndef CLIB_EXEC_PROTOS_H
  26. #include <clib/exec_protos.h>
  27. #include <pragmas/exec_pragmas.h>
  28. #endif
  29.  
  30. #ifndef EXEC_MEMORY_H
  31. #include <exec/memory.h>
  32. #endif
  33.  
  34. #ifndef CLIB_LOCALE_PROTOS_H
  35. #include <clib/locale_protos.h>
  36. #include <pragmas/locale_pragmas.h>
  37. #endif
  38.  
  39. /********************************************************************/
  40.  
  41. // some prototypes for the functions here
  42. // needed by SAS to create the library header
  43. int  __saveds __UserLibInit( void );
  44. void __saveds __UserLibCleanup( void );
  45.  
  46. struct Library *DOSBase;
  47. struct Library *AslBase;
  48. struct Library *GfxBase;
  49. struct Library *DOpusBase;
  50. struct Library *LocaleBase;
  51. struct Library *UtilityBase;
  52. struct Library *DiskfontBase;
  53. struct Library *GadToolsBase;
  54. struct Library *IntuitionBase;
  55.  
  56. #ifdef DATATYPES
  57. struct Library *DataTypesBase;
  58. #endif
  59. #ifdef AREXX
  60. struct Library *RexxSysBase; 
  61. #endif
  62.  
  63. // our module memorypool
  64. APTR mempool;
  65.  
  66. // since we want that this (and only this) process can not be
  67. // launched multiple times, we need something to check, if it is
  68. // already running. To pass arguments later to this process we
  69. // need the IPCData of this process for addressing. So we use
  70. // simply the IPCData to detect if the process is already running.
  71. // But take care: It is better to protect this variable with a
  72. // semaphore, if there is more "traffic" :)
  73.  
  74. IPCData *proc_ipc; 
  75.  
  76. // Locale pointer
  77. struct DOpusLocale *locale;
  78.  
  79. // a prototype from buildinstrings.c
  80. extern void init_locale_data(struct DOpusLocale *locale);
  81.  
  82. /********************************************************************/
  83.  
  84. // Library initialisation code
  85. int __saveds __UserLibInit()
  86. {
  87.         // Initialise pointers
  88.         AslBase = 0;
  89.         GfxBase = 0;
  90.         DOpusBase = 0;
  91.         LocaleBase = 0;
  92.         UtilityBase = 0;
  93.         DiskfontBase = 0;
  94.         GadToolsBase = 0;
  95.         IntuitionBase = 0;
  96.         locale = 0;
  97.         mempool = NULL;
  98.           proc_ipc = NULL; 
  99.                   
  100. #ifdef DATATYPES                  
  101.         DataTypesBase = 0;
  102. #endif            
  103. #ifdef AREXX
  104.         RexxSysBase = 0;
  105. #endif
  106.                                  
  107.         // Get DOS library (can't really fail)
  108.         DOSBase = OpenLibrary( "dos.library", 0 );
  109.  
  110.         // Open other libraries we need
  111.         if( !(AslBase = OpenLibrary("asl.library", 37))              ||
  112.             !(GfxBase = OpenLibrary("graphics.library", 37))         ||
  113.             !(DOpusBase = OpenLibrary("dopus5.library", 55))         ||
  114.             !(UtilityBase = OpenLibrary("utility.library", 37))      ||            
  115.             !(GadToolsBase = OpenLibrary( "gadtools.library", 37))   ||
  116.             !(IntuitionBase = OpenLibrary( "intuition.library", 37)) ||
  117.                                 
  118. #ifdef DATATYPES                                
  119.             !(DataTypesBase = OpenLibrary( "datatypes.library", 39)) ||
  120. #endif                          
  121. #ifdef AREXX
  122.             !(RexxSysBase = OpenLibrary("rexxsyslib.library", 0))    ||
  123. #endif      
  124.             !(DiskfontBase = OpenLibrary("diskfont.library", 37)) )
  125.              return 1;
  126.         
  127.         // Creating our memorypool and use it immediate for the locale
  128.         if( !(mempool = NewMemHandle(4096, 3072, MEMF_CLEAR|MEMF_PUBLIC)) ||
  129.             !(locale = AllocMemH(mempool, sizeof(struct DOpusLocale))) )
  130.              return 1;
  131.  
  132.         init_locale_data(locale);
  133.  
  134.         // Open locale library
  135.         if( LocaleBase = OpenLibrary("locale.library", 38) )
  136.           {
  137.              // Store library pointer
  138.              locale->li_LocaleBase = LocaleBase;
  139.  
  140.              // Open catalog if name supplied
  141.              if( module_info.locale_name )
  142.                {
  143.                   struct TagItem tags[2];
  144.  
  145.                   // If MODULEF_CATALOG_VERSION is set, we do version checking
  146.                   tags[0].ti_Tag = (module_info.flags & MODULEF_CATALOG_VERSION) ? OC_Version : TAG_IGNORE;
  147.                   tags[0].ti_Data = module_info.ver;
  148.                   tags[1].ti_Tag = TAG_DONE;
  149.  
  150.                   // Open catalog
  151.                   locale->li_Catalog = OpenCatalogA( NULL, module_info.locale_name, tags );
  152.                }
  153.  
  154.              // Get default lolale
  155.              locale->li_Locale = OpenLocale( 0 );
  156.           }
  157.         
  158.         return NULL; // Succeeded
  159. }
  160.  
  161.  
  162. // Clean up
  163. void __saveds __UserLibCleanup()
  164. {
  165.      if( mempool )
  166.        {
  167.           if( locale )
  168.             {
  169.                if( LocaleBase )
  170.                  {
  171.                     CloseLocale( locale->li_Locale );
  172.                     CloseCatalog( locale->li_Catalog );
  173.                     CloseLibrary( LocaleBase );
  174.                  }
  175.  
  176.                FreeMemH(locale);
  177.             }
  178.  
  179.           FreeMemHandle( mempool );
  180.        }
  181.                   
  182.      // Close libraries
  183.      CloseLibrary( AslBase );
  184.      CloseLibrary( GfxBase );
  185.      CloseLibrary( DOpusBase );
  186.      CloseLibrary( DiskfontBase );
  187.      CloseLibrary( UtilityBase );
  188.                   
  189. #ifdef DATATYPES
  190.      CloseLibrary( DataTypesBase );
  191. #endif            
  192. #ifdef AREXX              
  193.      CloseLibrary( RexxSysBase );
  194. #endif        
  195.  
  196.      CloseLibrary( GadToolsBase );
  197.      CloseLibrary( IntuitionBase );
  198.      CloseLibrary( DOSBase );
  199. }
  200.  
  201. /********************************************************************/
  202.  
  203. // This routine is called by DOpus to find out what the module does
  204. // Do not modify it or move it to an other place !!
  205.  
  206. ModuleInfo *__asm __saveds L_Module_Identify( register __d0 int num )
  207. {
  208.      // Return module information
  209.      if( num == -1 )
  210.           return &module_info;
  211.  
  212.      // Valid function number?
  213.      if( num > module_info.function_count ||
  214.          !(module_info.function[num].desc) )
  215.           return 0;
  216.  
  217.      // Return function description
  218.      return (ModuleInfo *) DOpusGetString( locale, module_info.function[num].desc );
  219. }
  220.  
  221. /********************************************************************/
  222.  
  223.  
  224.  
  225.